//
//  ActivityCategoryViewController.m
//  AttendenceSys
//
//  Created by ld on 16/4/12.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ActivityCategoryViewController.h"

@interface ActivityCategoryViewController ()

@end

@implementation ActivityCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self initView];
    
    // Do any additional setup after loading the view.
}

-(void)initData
{
    _actCategoryArr = [[NSMutableArray alloc] init];
    [self getActCategoryData];
}

//获取活动类别
-(void)getActCategoryData
{
    //获取活动科目类别 http://211.65.216.54/getActionSubject.action
    NSString* refer = @"getActionSubject.action?";
    NSString* prefix = kASHttpUrl(refer);
    
    [SVProgressHUD showWithStatus:@"活动类别" maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:prefix parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        jsonRsp = [jsonRsp processDictionaryIsNSNull:jsonRsp];
        
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            _actCategoryArr = [self parseActCategoryListInfo:data];
            [self.tableView reloadData];
            [SVProgressHUD dismiss];
            
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView reloadData];
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

//解析活动列表
-(NSMutableArray*)parseActCategoryListInfo:(NSArray*)data
{
    NSMutableArray* actArray = [[NSMutableArray alloc] init];
    for (int i=0; i<data.count; i++) {
        ActivitiesInfo* actInfo = [[ActivitiesInfo alloc] init];
        NSDictionary* dic = [data objectAtIndex:i];
        dic = [dic processDictionaryIsNSNull:dic];
        actInfo.subjectId = [dic objectForKey:@"subjectId"];
        actInfo.subjectName = [dic objectForKey:@"subjectName"];
        
        [actArray addObject:actInfo];
    }
    return actArray;
}

-(void)initView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[AskForLeaTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AskForLeaTableViewCell class])];
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_actCategoryArr count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AskForLeaTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AskForLeaTableViewCell class])];
    [cell setBackgroundColor:[UIColor clearColor]];
    ActivitiesInfo* actInfo = [_actCategoryArr objectAtIndex:indexPath.row];
    
    if (!actInfo.subjectName) {
        actInfo.subjectName = @"";
    }
    
    cell.titleLabel.text = actInfo.subjectName;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ActivitiesInfo* actvisitInfo = [_actCategoryArr objectAtIndex:indexPath.row];
    
    // 判断是否是实习单位获取
    if (2 == AS_APP_UserType)
    {
        ASPracUnitSchoolActivitiesVC *actVC = [[ASPracUnitSchoolActivitiesVC alloc] init];
        actVC.hidesBottomBarWhenPushed = YES;
        actVC.actInfo = actvisitInfo;
        [self.navigationController pushViewController:actVC animated:YES];
        return;
    }
    
    SchoolActivitiesViewController* actViewCtrl = [[SchoolActivitiesViewController alloc] init];
    actViewCtrl.hidesBottomBarWhenPushed = YES;
    actViewCtrl.actInfo = actvisitInfo;
    [self.navigationController pushViewController:actViewCtrl animated:YES];
    
//    if (0 == indexPath.row) {
//        ApplyForClassLeaveViewController* appForClassViewCtrl = [[ApplyForClassLeaveViewController alloc] init];
//        appForClassViewCtrl.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:appForClassViewCtrl animated:YES];
//    }
//    
//    if (1 == indexPath.row) {
//        ApplyForOutHolidayViewController* appForForOutHolidayViewCtrl = [[ApplyForOutHolidayViewController alloc] init];
//        appForForOutHolidayViewCtrl.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:appForForOutHolidayViewCtrl animated:YES];
//    }
//    
//    if (2 == indexPath.row) {
//        CancleOutHolidayViewController* detailViewCtrl = [[CancleOutHolidayViewController alloc] init];
//        [self.navigationController pushViewController:detailViewCtrl animated:YES];
//    }
    
}

-(void)dealloc
{
    _tableView = nil;
    _actCategoryArr = nil;
}

@end
